package com.yft.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

public class DatetimeUtils {

	public DatetimeUtils() {
	}

	public static String format(Object time, String format) {
		if (time == null) {
			return "";
		} else {
			SimpleDateFormat formattm = new SimpleDateFormat(format);
			return formattm.format(time);
		}
	}

	public static Object getCurrDateTime() {
		Calendar cal = Calendar.getInstance();
		return cal.getTime();
	}

	public static Date getNow() {
		Calendar cal = Calendar.getInstance();
		return cal.getTime();
	}

	public static String getCurrentDateTime(String format) {
		return format(getCurrDateTime(), format);
	}

	public static String getNextDateTime(int afterToday, String format) {
		Calendar cal = Calendar.getInstance();
		cal.add(5, afterToday);
		return format(cal.getTime(), format);
	}

	public static String getNextDateTime(Date date, int diff, String format) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(5, diff);
		return format(cal.getTime(), format);
	}

	public static Date getNextDateTime(int afterToday) {
		Calendar cal = Calendar.getInstance();
		cal.add(5, afterToday);
		return cal.getTime();
	}

	public static String getNextMonthTime(int afterMonth, String format) {
		Calendar cal = Calendar.getInstance();
		cal.add(2, afterMonth);
		return format(cal.getTime(), format);
	}

	public static Date getNextMonthTime(int afterMonth) {
		Calendar cal = Calendar.getInstance();
		cal.add(2, afterMonth);
		return cal.getTime();
	}

	public static Date string2date(String source) throws ParseException {
		source = StringUtils.trimToNull(source);
		if (source == null) {
			return null;
		} else {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			return df.parse(source);
		}
	}

	public static Date string2datetime(String source) throws ParseException {
		source = StringUtils.trimToNull(source);
		if (source == null)
			return null;
		if (!StringUtils.contains(source, " ")
				&& !StringUtils.contains(source, ":"))
			return string2date(source);
		if (StringUtils.split(source, ":").length == 2)
			source = (new StringBuilder(String.valueOf(source))).append(":00")
					.toString();
		if (StringUtils.contains(source, " ")
				&& !StringUtils.contains(source, ":"))
			source = (new StringBuilder(String.valueOf(source))).append(
					":00:00").toString();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.parse(source);
	}

	public static Date string2date(String source, String format)
			throws ParseException {
		if (StringUtils.trimToNull(source) == null) {
			return null;
		} else {
			SimpleDateFormat df = new SimpleDateFormat(format);
			return df.parse(source);
		}
	}

	public static Timestamp string2timestamp(String source)
			throws ParseException {
		if (StringUtils.trimToNull(source) == null)
			return null;
		else
			return new Timestamp(string2datetime(source).getTime());
	}

	public static Timestamp string2timestamp(String source, String format)
			throws ParseException {
		if (StringUtils.trimToNull(source) == null)
			return null;
		else
			return new Timestamp(string2date(source, format).getTime());
	}

	public static int getDayOfWeek() {
		return getDayOfWeek(null);
	}

	public static int getDayOfWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		if (date != null)
			cal.setTime(date);
		return cal.get(7) - 1;
	}

	public static Date[] getWeekDate(Date date) {
		return getWeekDate(date, 1);
	}

	public static Date[] getWeekDate(int weeks) {
		return getWeekDate(null, weeks);
	}

	public static Date[] getWeekDate(Date date, int weeks) {
		if (weeks < 1)
			weeks = 1;
		int days = 7;
		days *= weeks;
		Date weekdate[] = new Date[days];
		Calendar cal = Calendar.getInstance();
		if (date != null)
			cal.setTime(date);
		int day_of_week = cal.get(7) - 1;
		cal.add(5, -day_of_week);
		Date dt = cal.getTime();
		cal.clear();
		for (int i = 0; i < days; i++) {
			Calendar cal_temp = Calendar.getInstance();
			cal_temp.setTime(dt);
			cal_temp.add(5, i);
			weekdate[i] = cal_temp.getTime();
			cal_temp.clear();
		}

		return weekdate;
	}

	public static String getAM_PM(String timeString) {
		String time[] = StringUtils.split(timeString, ":");
		if (time != null && time.length > 1) {
			int hour = NumberUtils.toInt(time[0]);
			if (hour >= 0 && hour < 13)
				return "AM";
			if (hour >= 13 && hour < 24)
				return "PM";
			else
				return "ERROR";
		} else {
			return "ERROR";
		}
	}

	public static Calendar string2calendar(String date) {
		String dt[] = StringUtils.split(date, "-");
		int year = Integer.parseInt(dt[0]);
		int month = Integer.parseInt(dt[1]);
		int day = Integer.parseInt(dt[2]);
		Calendar cal = Calendar.getInstance();
		cal.set(year, month - 1, day);
		return cal;
	}
	
	public static Calendar beginOfDay(Calendar cal){
		cal.set(Calendar.HOUR_OF_DAY,0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND,0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal;
	}
	
	public static Date beginOfDay(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY,0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND,0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	public static Calendar endOfDay(Calendar cal){
		cal.set(Calendar.HOUR_OF_DAY,23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND,59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal;
	}
	
	
	public static Date endOfDay(Date endDt){
		Calendar cal = Calendar.getInstance();
		cal.setTime(endDt);
		cal.set(Calendar.HOUR_OF_DAY,23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND,59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}
	
	public static List<Date> getDates(Date beginDate,Date endDate,int diffType,int diff){
		Calendar cal = Calendar.getInstance();
		List<Date> dates = new ArrayList<Date>();
	
		while (beginDate.before(endDate)){
			dates.add(beginDate);
			
			cal.setTime(beginDate);
			cal.add(diffType, diff);
			beginDate = cal.getTime();
		}
		
		dates.add(endDate);
		
		return dates;
		
	}
	
	public static void main(String[] args) throws Exception{
		Date beginDate = DatetimeUtils.string2datetime("2009-05-01 2:23:12");
		Date endDate = new Date();
		List<Date> dates = getDates(beginDate,endDate,Calendar.HOUR,1);
		
		for (Date d:dates){
			System.out.println(DatetimeUtils.format(d, "yyyy-MM-dd HH:mm:ss"));
		}
		
	}
}
